package com.dify.knowledge.repository;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dify.knowledge.dto.UnansweredQuestionDTO;
import com.dify.knowledge.entity.UnansweredQuestion;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

/**
 * 未解答问题Mapper接口
 */
@Mapper
public interface UnansweredQuestionMapper extends BaseMapper<UnansweredQuestion> {
    
    /**
     * 分页查询未解答问题（状态为PENDING），联表查询问题详情和解答数量
     * @param page 分页对象
     * @return 分页结果
     */
    @Select("SELECT uq.id, uq.question_record_id, uq.status, uq.created_at, " +
            "qr.user_id, qr.question, " +
            "COALESCE(COUNT(ma.id), 0) as answer_count " +
            "FROM unanswered_question uq " +
            "LEFT JOIN question_record qr ON uq.question_record_id = qr.id " +
            "LEFT JOIN manual_answer ma ON uq.id = ma.unanswered_question_id " +
            "WHERE uq.status = 'PENDING' " +
            "GROUP BY uq.id, uq.question_record_id, uq.status, uq.created_at, qr.user_id, qr.question " +
            "ORDER BY uq.created_at DESC")
    Page<UnansweredQuestionDTO> selectUnanswered(Page<UnansweredQuestionDTO> page);
}
